﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace BAL.EstRec.FDVUtil
{
    public class VMPublish : INotifyPropertyChanged
    {
        System.Timers.Timer t;
        CollectionsPivotFDV cpfdv;
        bool _jobDone;
        bool _manualClose;
        string _message;
        long unidVersion;
        PublishFDV objPubFDV;
        System.Windows.Controls.Label lblStatus;
        public static System.Threading.Thread thrd;
        System.Windows.Controls.Button btnPublish;

        public bool JobDone
        {
            get { return _jobDone; }
            set
            {
                if (value != _jobDone)
                {
                    this._jobDone = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("JobDone"));
                    }
                }
            }
        }

        public bool ManualClose
        {
            get { return _manualClose; }
            set
            {
                if (value != _manualClose)
                {
                    this._manualClose = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ManualClose"));
                    }
                }
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                if (value != _message)
                {
                    this._message = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Message"));
                    }
                }
            }
        }


        public void publish(Object sender, System.Timers.ElapsedEventArgs args)
        {
            this.t.Enabled = false;
            ((System.Timers.Timer)sender).Stop();
            this.objPubFDV.Publish(this);
            //this.Message = "Información publicada." + this.getFailedXLS();
            this.JobDone = true;

            VMPublish.thrd = new System.Threading.Thread(() =>
            {
                Action a = () => this.lblStatus.Content = validateVersionPublishStatus();
                Application.Current.Dispatcher.BeginInvoke(a, System.Windows.Threading.DispatcherPriority.Background);
            });

            VMPublish.thrd.IsBackground = true;
            VMPublish.thrd.Start();

        }

        public VMPublish(CollectionsPivotFDV cpfdv, long unidVersion, System.Windows.Window wnd)
        {
            this._jobDone = false;
            this._manualClose = false;
            this._message = "Publicando...";
            this.cpfdv = cpfdv;
            this.unidVersion = unidVersion;
            this._jobDone = false;
            this.objPubFDV = new PublishFDV(unidVersion, wnd);
            t = new System.Timers.Timer(100);
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(publish);

        }

        public VMPublish(CollectionsPivotFDV cpfdv, long unidVersion, System.Windows.Window wnd, System.Windows.Controls.Label lbl, System.Windows.Controls.Button btn)
        {

            //System.Windows.Controls.Label lbl = (System.Windows.Controls.Label)this.w..FindName("lblStatus");
            this.lblStatus = lbl;
            this.btnPublish = btn;

            this._jobDone = false;
            this._manualClose = false;
            this._message = "Publicando...";
            this.cpfdv = cpfdv;
            this.unidVersion = unidVersion;
            this._jobDone = false;
            this.objPubFDV = new PublishFDV(unidVersion, wnd);
            t = new System.Timers.Timer(100);
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(publish);

        }

        public void start()
        {
            t.Start();
        }

        private string getFailedXLS()
        {
            string sXls = "";
            if (this.objPubFDV.failedXls.Count > 0)
            {
                this.objPubFDV.failedXls.ForEach(
                       f => sXls += f
                   );

            }
            return sXls;
        }

        private string validateVersionPublishStatus()
        {
            if (this.unidVersion != null)
            {
                string mensaje = "";
                try
                {
                    if (Convert.ToBoolean(this.objPubFDV.GetPublishStatusVersion(this.unidVersion)))
                    {
                        mensaje = "Versión publicada";
                        this.btnPublish.IsEnabled = false;
                    }
                    else
                    {
                        mensaje = "No se pudo publicar la versión.";
                    }

                }
                catch (Exception ex)
                {
                    mensaje = "No se pudo verificar la publicación. " + ex.Message;
                }

                return mensaje;
            }
            else return "No se pudo recuperar la versión actual para validar su publicación.";
        }//

        public event PropertyChangedEventHandler PropertyChanged;
    }// end class
}
